# -*- coding:utf-8 -*-
__author__ = 'Serprutiy'

import time
from functools import wraps


def timeit(func):

    @wraps(func)                            # 保留被装饰函数的特殊属性
    def wrapper(*args, **kwargs):
        start = time.clock()
        func(*args, **kwargs)
        end = time.clock()
        print('used: %s' % (end - start))

    return wrapper

@timeit
def foo(name):
    print('in foo()')
    print('input name', name)


# 三个内置的装饰器理解
# staticmethod  :将类中定义的实例方法变成静态方法
# classmethod   :将类中定义的实例方法变成类方法
# property      :将类中定义的实例方法变成类属性(只读的)
class MyClass(object):
    def __init__(self):                 # 初始化函数，相当于c++中的构造函数
        print('init')
        self._name = 'haohaoxuexi'

    @staticmethod                       # 静态函数，可以直接通过类调用
    def static_method():
        print('This is a static method.')

    @classmethod
    def class_method(cls):              # 类函数，也可以直接通过类调用，cls为类
        print('This is a class method', cls)
        print('Visit the property of the class', cls.name)
        instance = cls()                # cls()就是实例化一个类对象
        print('Visit the normal method of the class: %s' % instance.test())
        print('The name:', instance._name, instance.name)

    def test(self):
        print('call test')

    @property                           # 将类实例函数转换为类属性
    def name(self):
        return self._name

    @name.setter
    def name(self, value):
        self._name = value

    @name.deleter
    def name(self):
        del self._name


if __name__ == '__main__':
    # MyClass.static_method()
    # MyClass.class_method()
    mc = MyClass()
    print(mc.name, mc._name)
    mc._name = 'safeguard'
    mc.name = 'setter'
    print(mc.name, mc._name)
    del mc.name